package com.jiuyou.clientController.shape;

import com.jiuyou.clientController.base.ClientBaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.MemberTokenService;
import com.ruoyi.shape.domain.Shape;
import com.ruoyi.shape.domain.ShapeUser;
import com.ruoyi.shape.service.IShapeService;
import com.ruoyi.shape.service.IShapeUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/client/shape")
public class ShapeClientController extends ClientBaseController {

    @Autowired
    private MemberTokenService tokenService;

    @Autowired
    private IShapeService shapeService;

    @Autowired
    private IShapeUserService shapeUserService;

    /**
     * 查询手型信息列表
     */
    @GetMapping("/getHandShapeList")
    public TableDataInfo getHandShapeList(Shape shape)
    {
        startPage();
        List<Shape> list = shapeService.selectHandShapeList(shape);
        return getDataTable(list);
    }

    /**
     * 查询脸型信息列表
     */
    @GetMapping("/getFaceShapeList")
    public TableDataInfo getFaceShapeList(Shape shape)
    {
        startPage();
        List<Shape> list = shapeService.selectFaceShapeList(shape);
        return getDataTable(list);
    }


    /**
     * 添加用户选择手型或脸型
     * */
    @PostMapping("/createUserShape")
    public AjaxResult create(Long shapeId){
        if(shapeId == null){
            return AjaxResult.error("shapeId不能为空");
        }
        Shape shapeOne = shapeService.selectShapeById(shapeId);
        if(shapeOne == null){
            return AjaxResult.error("没有这个手型或脸型");
        }

        Long memberid=tokenService.getLoginMemberId();

        ShapeUser shapeUser = new ShapeUser();
        shapeUser.setShapeId(shapeId);
        shapeUser.setUserId(memberid);
        shapeUser.setType(shapeOne.getType());
        shapeUserService.insertShapeUser(shapeUser);

        return AjaxResult.success();
    }


    /**
     * 用户选中的手型
     * */
    @PostMapping("/selectHandShape")
    public AjaxResult selectHandShape(){
        Long memberid=tokenService.getLoginMemberId();

        ShapeUser shapeUserone = shapeUserService.selectShapeUserByUserIdHand(memberid);
        if(shapeUserone == null){
            return AjaxResult.error("没有选中的手型");
        }
        Shape shapeOne = shapeService.selectShapeById(shapeUserone.getShapeId());
        if(shapeOne == null){
            return AjaxResult.error("没有手型信息表");
        }

        return AjaxResult.success(shapeOne);
    }

    /**
     * 用户选中的脸型
     * */
    @PostMapping("/selectFaceShape")
    public AjaxResult selectFaceShape(){
        Long memberid=tokenService.getLoginMemberId();

        ShapeUser shapeUserone = shapeUserService.selectShapeUserByUserIdFace(memberid);
        if(shapeUserone == null){
            return AjaxResult.error("没有选中的脸型");
        }
        Shape shapeOne = shapeService.selectShapeById(shapeUserone.getShapeId());
        if(shapeOne == null){
            return AjaxResult.error("没有脸型信息表");
        }

        return AjaxResult.success(shapeOne);
    }



}
